package com.bawei.library_image.imageloader

import android.content.Context
import android.graphics.drawable.Drawable
import android.net.Uri
import android.widget.ImageView
import com.bawei.library_image.IImageLoader
import com.bawei.library_image.ImageCrop
import com.bawei.library_image.ImageOptions
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import java.io.File
import java.lang.ref.WeakReference

/**
 * @Author : 
 * @Time : On 2023/6/20 15:35
 * @Description : GlideImageLoader Glide图片加载器
 */
class GlideImageLoader:IImageLoader {
    override fun loadFileIntoImageView(
        context: Context,
        file: File,
        target: ImageView,
        config: ImageOptions,
    ) {
        loadImageView(context,file,target,config)
    }

    override fun loadUrlIntoImageView(
        context: Context,
        url: String,
        target: ImageView,
        config: ImageOptions,
    ) {
        loadImageView(context,url,target,config)
    }

    override fun loadResourceIntoImageView(
        context: Context,
        source: Int,
        target: ImageView,
        config: ImageOptions,
    ) {
        loadImageView(context,source,target,config)
    }

    override fun loadUriIntoImageView(
        context: Context,
        uri: Uri,
        target: ImageView,
        config: ImageOptions,
    ) {
        loadImageView(context,uri,target,config)
    }

    override fun loadByteArrayIntoImageView(
        context: Context,
        bytes: Array<Byte>,
        target: ImageView,
        config: ImageOptions,
    ) {
        loadImageView(context,bytes,target,config)
    }

    private fun loadImageView(
        context: Context,
        source: Any,
        target: ImageView,
        config: ImageOptions,
    ) {
        //解决内存泄漏问题。传入的activity的时候很容易出现内存泄露
        var weakReference = WeakReference<Context>(context)
        if(weakReference.get() == null){
            return
        }
        val builder = Glide.with(context).load(source)
        //解析ImageOptions参数为builder设置
        setBuilderOptions(builder,config)
        builder.into(target)
    }
    //设置Glide参数
    private fun setBuilderOptions(builder: RequestBuilder<Drawable>, config: ImageOptions) {

        config.let {
            val options = RequestOptions()
            if(config.placeImage != 0){
                options.placeholder(config.placeImage)
            }
            if(config.errorImage != 0){
                options.error(config.errorImage)
            }
            config.imageSize.let {
                if(config.imageSize.size != 2){
                    throw IllegalArgumentException("please set imageSize length size is 2")
                }
                options.override(config.imageSize[0],config.imageSize[1])//图片压缩大小，可以解决内存溢出
            }
            if(config.skipDiskCache) {//关闭磁盘缓存
                options.diskCacheStrategy(DiskCacheStrategy.NONE)
            }
            if(config.skipMemoryCache){//关闭内存缓存
                options.skipMemoryCache(true)
            }
            //裁剪：策略模式
            when(config.imageCrop){
                ImageCrop.CENTER ->options.transform(CenterCrop())
                ImageCrop.CIRCLE ->options.transform(CircleCrop())
                ImageCrop.ROUNDED ->options.transform(CenterCrop(),RoundedCorners(20))
            }
            builder.apply(options)
        }
    }


}